package com.atguigu.gulimall.ware.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.Exchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author 小坏
 * @Date 2021/2/16 0:33
 * @Version 1.0
 * @program: 父工程 gulimall 万物起源之地
 * MQ json 序列号机制
 * <p>
 * 库存锁定队列
 */

@Configuration
public class MyWareRabbitConfig {

//    @RabbitListener(queues = "stock.release.stock.queue")
//    public void hander(Message message) {
//        System.out.println ("解锁订单" + message);
//    }

    /**
     * json 序列号机制
     *
     * @return
     */
    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter ();
    }


    /**
     * 库存服务MQ
     *
     * @return
     */
    @Bean
    public Exchange stockEventExchange() {
        return new TopicExchange ("stock-event-exchange", true, false);
    }


    @Bean
    public Queue stockReleaseStockQueue() {
        return new Queue ("stock.release.stock.queue", true, false, false);
    }

    /**
     * x-dcad-letter-exchange:stock- event-exchange
     * x-dead-letter-routing-key: order.release.order
     * x-message-ttl: 60000
     *
     * @return
     */

    @Bean
    public Queue stockDelayQueue() {
        Map<String, Object> args = new HashMap<> ();
        args.put ("x-dead-letter-exchange", "stock-event-exchange");
        args.put ("x-dead-letter-routing-key", "stock.release");
        args.put ("x-message-ttl", 120000);
        return new Queue ("stock.delay.queue", true, false, false, args);
    }

    @Bean
    public Binding stockReleaseBinding() {
        /**
         * String destination, DestinationType destinationType, String exchange, String routingKey,
         * 			Map<String, Object> arguments
         */
        return new Binding ("stock.release.stock.queue",
                Binding.DestinationType.QUEUE,
                "stock-event-exchange",
                "stock.release.#",
                null);
    }

    @Bean
    public Binding stockLockedBinding() {
        /**
         * String destination, DestinationType destinationType, String exchange, String routingKey,
         * 			Map<String, Object> arguments
         */
        return new Binding ("stock.delay.queue", Binding.DestinationType.QUEUE, "stock-event-exchange", "stock.locked",
                null);
    }


}
